//
//  WXSSleepInfoModel.m
//  SleepBooks
//
//  Created by 王勇 on 16/9/10.
//  Copyright © 2016年 rimi. All rights reserved.
//

#import "WXSSleepInfoModel.h"
#import "WXSSleepStatusModel.h"

#define kSleepDateKey @"sleepDateKey"
#define kStartTimeKey @"startTimeKey"
#define kEndTimeKey @"endTimeKey"
#define kTotalSleepingTimeKey @"totalSleepingTime"
#define kDeepSleepingTimeKey @"deepSleepingTIme"
#define kShallowSleepingTimeKey @"shallowSleepingTime"
#define kAwakeTimeKey @"awakeTime"
#define kDreamTalkModelKey @"dreamTalkModel"
#define kSleepStatusSequenceKey @"sleepStatusSequence"
#define kAwakeModelsKey @"awakeModels"
#define kShallowModelsKey @"shallowModels"
#define kDeepModelsKey @"deepModels"

@implementation WXSSleepInfoModel

-(instancetype)initWithCoder:(NSCoder *)aDecoder{
    self.sleepDate = [aDecoder decodeObjectForKey:kSleepDateKey];
    self.startTime = [aDecoder decodeObjectForKey:kStartTimeKey];
    self.endTime = [aDecoder decodeObjectForKey:kEndTimeKey];
    self.totalSleepingTime = [aDecoder decodeDoubleForKey:kTotalSleepingTimeKey];
    self.deepSleepingTime = [aDecoder decodeDoubleForKey:kDeepSleepingTimeKey];
    self.shallowSleepingTime = [aDecoder decodeDoubleForKey:kShallowSleepingTimeKey];
    self.awakeTime = [aDecoder decodeDoubleForKey:kAwakeTimeKey];
    self.dreamTalkModels = [aDecoder decodeObjectForKey:kDreamTalkModelKey];
    self.sleepStatusSequence = [aDecoder decodeObjectForKey:kSleepStatusSequenceKey];
    self.awakeModels = [aDecoder decodeObjectForKey:kAwakeModelsKey];
    self.shallowModels = [aDecoder decodeObjectForKey:kShallowModelsKey];
    self.deepModels = [aDecoder decodeObjectForKey:kDeepModelsKey];
    return self;
}

-(void)encodeWithCoder:(NSCoder *)aCoder{
    [aCoder encodeObject:self.sleepDate forKey:kSleepDateKey];
    [aCoder encodeObject:self.startTime forKey:kStartTimeKey];
    [aCoder encodeObject:self.endTime forKey:kEndTimeKey];
    [aCoder encodeDouble:self.totalSleepingTime forKey:kTotalSleepingTimeKey];
    [aCoder encodeDouble:self.deepSleepingTime forKey:kDeepSleepingTimeKey];
    [aCoder encodeDouble:self.shallowSleepingTime forKey:kShallowSleepingTimeKey];
    [aCoder encodeDouble:self.awakeTime forKey:kAwakeTimeKey];
    [aCoder encodeObject:self.dreamTalkModels forKey:kDreamTalkModelKey];
    [aCoder encodeObject:self.sleepStatusSequence forKey:kSleepStatusSequenceKey];
    [aCoder encodeObject:self.awakeModels forKey:kAwakeModelsKey];
    [aCoder encodeObject:self.shallowModels forKey:kShallowModelsKey];
    [aCoder encodeObject:self.deepModels forKey:kDeepModelsKey];
}

-(NSTimeInterval)deepSleepingTime{
    NSTimeInterval sumOfDeepSleep = 0;
    //遍历所有的deepModel，找到每一段deep的timeInterval，求和
    for (WXSDeepSleepStatusModel *deepModel in self.deepModels) {
        sumOfDeepSleep += deepModel.statusDuration;
    }
    return sumOfDeepSleep;
}

-(NSTimeInterval)shallowSleepingTime{
    NSTimeInterval sumOfShallowSleep= 0;
    //遍历所有的shallowModel，找到每一段shallow的timeInterval，求和
    for (WXSShallowSleepStatusModel *shallowModel in self.shallowModels) {
        sumOfShallowSleep += shallowModel.statusDuration;
    }
    
    return sumOfShallowSleep;
}

-(NSTimeInterval)awakeTime{
    NSTimeInterval sumOfAwakeTime;
    for (WXSAwakeSleepStatusModel *awakeModel in self.awakeModels) {
        sumOfAwakeTime += awakeModel.statusDuration;
    }
    return sumOfAwakeTime;
}

#pragma mark - Private 

#pragma mark Getter & Setter
-(NSMutableArray<WXSSleepStatusModel *> *)sleepStatusSequence{
    if (!_sleepStatusSequence) {
        _sleepStatusSequence = [NSMutableArray array];
    }
    return _sleepStatusSequence;
}

-(NSMutableArray<WXSDeepSleepStatusModel *> *)deepModels{
    if (!_deepModels) {
        _deepModels = [NSMutableArray array];
    }
    return _deepModels;
}

-(NSMutableArray<WXSShallowSleepStatusModel *> *)shallowModels{
    if (!_shallowModels) {
        _shallowModels = [NSMutableArray array];
    }
    return _shallowModels;
}

-(NSMutableArray<WXSAwakeSleepStatusModel *> *)awakeModels{
    if (!_awakeModels) {
        _awakeModels = [NSMutableArray array];
        
    }
    return _awakeModels;
}

-(NSMutableArray<WXSDreamTalkModel *> *)dreamTalkModels{
    if (!_dreamTalkModels) {
        _dreamTalkModels = [NSMutableArray array];
    }
    return _dreamTalkModels;
}


//-initwith

@end
